package com.kuaidu.ll.quickreadnews.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;

/**
 * Created by liulei on 2017/7/10.
 * fragment的基类
 */

public abstract class BaseFragment extends Fragment {

    public Context baseFragmentContext;
    private FragmentManager supportFragmentManager;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.baseFragmentContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeLoadView();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        beforeInit(view, savedInstanceState);
        init(view, savedInstanceState);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isResumed() && getUserVisibleHint()) {
            loadInitData();
        }
    }

    private void loadInitData() {
    }

    protected void beforeLoadView() {
        supportFragmentManager = getActivity().getSupportFragmentManager();

    }

    protected void beforeInit(View view, Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
    }

    private final void init(View view, Bundle saveInstanceState) {
        initView(view, saveInstanceState);
        initData(saveInstanceState);
    }

    public void showToast(String message) {
        showToast(message, Toast.LENGTH_SHORT);
    }

    public void showToast(int id) {
        Context context = getContext();
        if (isShowToast(context)) {
            return;
        }
        String message = context.getResources().getString(id);
        showToast(message);
    }

    public void showToast(String message, int length) {
        Context context = getContext();
        if (isShowToast(context)) {
            return;
        }
        Toast.makeText(context, message, length).show();
    }

    public void showToast(int id, int length) {
        Context context = getContext();
        if (isShowToast(context)) {
            return;
        }
        String message = baseFragmentContext.getResources().getString(id);
        showToast(message, length);
    }

    private boolean isShowToast(Context context) {
        if (getActivity() == null || getActivity().isFinishing() || context == null) {
            return false;
        }
        return true;
    }

    protected void popBackFragment() {
        if (supportFragmentManager == null) {
            supportFragmentManager = getActivity().getSupportFragmentManager();
        }
        int backStackEntryCount = supportFragmentManager.getBackStackEntryCount();
        if (backStackEntryCount > 0) {
            supportFragmentManager.popBackStack();
        }
        {
            getActivity().finish();
        }
    }



    protected abstract void initView(View view, Bundle saveInstanceState);

    protected abstract void initData(Bundle v);

    protected abstract int getLayoutId();
}
